<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="轮播图类型" prop="theType">
        <el-select
          v-model="queryParams.theType"
          @change="handleQuery"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in theTypeOptions"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="关键字" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入标题"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-hasPermi="['141010']"
          @click="handleAdd"
          >添加</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList" :border="true">
      <el-table-column label="轮播图类型" align="center"  prop="theType">
        <template slot-scope="scope">
          <span>{{ findTheType(scope.row.theType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column align="center" prop="thePic" label="banner图">
        <template slot-scope="scope">
          <imagePreview :src="scope.row.thePic" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" key="enable">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.enable"
            :active-value="1"
            :inactive-value="0"
            @change="handlefileChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['141020']"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['141030']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pagesize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="轮播图类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择">
            <el-option
              v-for="item in theTypeOptions"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="banner图" prop="headPic">
          <uploadImage :theType="13" v-model="form.thePic" />
        </el-form-item>
        <el-form-item label="位置排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            controls-position="right"
            :min="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadImage from "@/components/ImageUpload";
import { listData, getData, delData, addData, updateData, getEnable} from "@/api/system/banner";
import { getDataListBannerType} from "@/api/select";

export default {
  name: "banner",
  components: {
    UploadImage,
  },
  data() {
    return {
      loading: true,
      dataList: [],
      title: "",
      open: false,
      form: {},
      queryParams: {
        currentPage: 1,
        pagesize: 10,
        keywords: undefined,
        theType: undefined,  //轮播图类型 1-pc端首页 2-其他
      },
      // 显示搜索条件
      showSearch: true,
      total: 0,
      rules: {
        typeId: [
          { required: true, message: "请选择轮播图类型", trigger: "change" },
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" },
        ],
        thePic: [
          { required: true, message: "banner图不能为空", trigger: "blur" },
        ]
      },
      theTypeOptions: [],  //轮播图类型 1-pc端首页 2-其他
    };
  },
  watch: {},
  created() {
    this.getSelect();
    this.getList();
  },
  methods: {
    getSelect() {
      getDataListBannerType().then((response) => {
        if (response.code===200) {
          this.theTypeOptions = response.result.data
          console.log('this.theTypeOptions',this.theTypeOptions)
        }
      })
    },
    getList() {
      this.loading = true;
      listData(this.queryParams).then((response) => {
        this.dataList = response.result.rows;
        this.total = response.result.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        typeId: undefined,
        typeVal: undefined,
        title: undefined,
        thePic: undefined,
        sort: 0,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const theId = row.theid;
      getData(theId).then((response) => {
        this.form = response.result;
        this.$set(this.form, "typeId", response.result.theType);

        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.theid != undefined) {
            updateData(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const theId = row.theid;
      this.$confirm('是否确认删除?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delData(theId);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    handlefileChange(row) {
      let text = row.enable === 1 ? "启用" : "停用";
      this.$confirm('是否确认' + text + '"?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          // 调接口 改变状态值
          return getEnable(row.theid, row.enable);
        })
        .then(() => {
          this.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.enable = row.enable === 1 ? 0 : 1;
        });
    },
    changeType(val) {
      this.form.typeVal = val
        ? this.theTypeOptions.find((ele) => ele.id === val).label
        : "";
    },
    findTheType(val) {
      if (this.theTypeOptions.length > 0) {
        let value = val
          ? this.theTypeOptions.find((ele) => ele.id === val).label
          : "";
        return value;
      }
    },
  },
};
</script>

<style scoped lang="scss">
/* /deep/ .el-table {
  margin-left: 100px;
} */
/* treeselect */
.vue-treeselect__input {
  display: block;
}
/deep/ .vue-treeselect__control {
  height: 32px;
  line-height: 32px;
  margin-top: 3px;
}
</style>